<?php

class NewsLastest extends WP_Widget {

    function NewsLastest() {
        parent::WP_Widget(false, $name = 'Tinf5 - News Lastest Block');
    }

    function widget($args, $instance) {
        $title = $instance['title'];
        $number = $instance['number'];
        $count = count(allPostToday());
        ?>
        <input class="cat-0" name="cat[0]" value="<?php echo $number . '-0'; ?>" type="hidden"/>
        <div class="row">
            <p class="count countf5 count-three fl" title="Có <?php echo $count; ?> tin trong ngày hôm nay"><?php echo $count; ?></p>
            <h3 class="fl"><?php echo $title; ?></h3>
            <span class="glyphicon glyphicon-chevron-down fr btn-shcate"></span>
        </div>
        <div class="newslistwrap contentblock">
            <div class="newslist">
                <?php
                add_filter('posts_where', 'filter_where');
                query_posts(array('posts_per_page' => $number));
                $lastPost = 1;
                while (have_posts()) : the_post();
                    if ($lastPost == 1) {
                        ?><input id="lastpostID" value="<?php the_ID(); ?>" type="hidden"/><?php
                        $lastPost++;
                    }
                    echo getItemPostNews();
                endwhile;
                ?>

            </div>
        </div>
        <?php
        wp_reset_query();
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['number'] = ($new_instance['number']) ? $new_instance['number'] : 20;
        return $instance;
    }

    function form($instance) {
        $title = isset($instance['title']) ? $instance['title'] : '';
        $number = isset($instance['number']) ? $instance['number'] : '';
        ?>
        <fieldset class="ether-form">
            <div>
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
                <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
            </div>
            <div>
                <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number dislay:'); ?></label> 
                <input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
            </div>
        </fieldset>
        <?php
    }

}

register_widget('NewsLastest');
?>